package org.darkforest.common.services.impl;

import java.util.List;

import org.darkforest.common.services.HealthDeptmentService;
import org.darkforest.dao.mapper.HealthDeptMapper;
import org.darkforest.model.HealthDept;
import org.darkforest.vo.AjaxReturnData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

@Service
public class HealthDeptmentServiceImpl implements HealthDeptmentService {

    @Autowired
    private HealthDeptMapper healthDeptMapper;

    @Override
    @Transactional
    public int deleteHealthDeptment(String[] ids) {
        int delCount = 0;

        for (String id : ids) {
            // 先判断该数据是否已被删除
            HealthDept healthDeptment = healthDeptMapper.selectByPrimaryKey(Integer.parseInt(id));
            // 已被删除时，作为删除成功的数据来统计，因此删除件数+1
            if (healthDeptment == null) {
                delCount = delCount + 1;
            } else {
                // 删除发生异常时，抛出 RuntimeException,事物自动回滚
                int j = healthDeptMapper.deleteByPrimaryKey(Integer.parseInt(id));
                delCount = delCount + j;
            }
        }

        if (delCount != ids.length) {
            // 删除失败时，事物回滚
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return 0;
        }

        return 1;
    }

    @Override
    @Transactional
    public AjaxReturnData addHealthDeptList(List<HealthDept> healthDeptList) {
        AjaxReturnData ajaxReturnData = new AjaxReturnData();

        // 业务单元名称相同的报错，否则就添加
        HealthDept searchHealthDept = null;
        HealthDept healthDept = null;
        for (int i = 0; i<healthDeptList.size(); i++) {
            healthDept = healthDeptList.get(i);

            // 检索条件：业务单元名称
            searchHealthDept = new  HealthDept();
            searchHealthDept.setDeptName(healthDept.getDeptName());
            searchHealthDept.setPageSwitch(false);

            List<HealthDept> searchedList = healthDeptMapper.selectListByCondition(searchHealthDept);

            // 业务单元名称相同的数据没有
            if ((searchedList == null) || (searchedList.size() == 0)) {
                healthDeptMapper.insertSelective(healthDept);
            } else {
                // 数据库内有重复数据
                //  由于名称是LIKE检索，因此需要判断是否完全一致
                for (HealthDept dbDeptment :searchedList) {
                    // 当前名称和其他的记录的名称有相同时
                    if (dbDeptment.getDeptName().equals(healthDept.getDeptName())) {
                        // 添加失败时，事物回滚
                        TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                        ajaxReturnData.setResult(false);
                        ajaxReturnData.setMessage("数据库内有业务单元名称相同的数据");
                        return ajaxReturnData;
                    }
                }

                healthDeptMapper.insertSelective(healthDept);
            }
        }

        ajaxReturnData.setResult(true);
        ajaxReturnData.setMessage("导入成功");

        return ajaxReturnData;
    }
}
